package com.sh.cloudrender.application.web.exception;

import com.sh.cloudrender.common.constants.ErrorCode;
import com.sh.cloudrender.common.dto.ShXResponse;
import com.sh.cloudrender.common.utils.ShXResponseUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    /*@ExceptionHandler(MethodArgumentNotValidException.class)
    public ResponseEntity<Map<String, String>> handleValidationExceptions(MethodArgumentNotValidException ex) {
        Map<String, String> errors = new HashMap<>();
        ex.getBindingResult().getFieldErrors().forEach(error -> errors.put(error.getField(), error.getDefaultMessage()));
        return new ResponseEntity<>(errors, HttpStatus.BAD_REQUEST);
    }*/

    /**
     * 默认异常处理
     *
     * @param e 异常
     * @return 统一返回结果
     */
    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public ShXResponse<String> exceptionHandler(Exception e) {
        log.error("[全局异常]： ", e);
        return ShXResponseUtil.build(ErrorCode.ERROR400.getCode(), e.getMessage(), null);
    }

    @ExceptionHandler(MethodArgumentNotValidException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public ShXResponse<String> exceptionHandler(MethodArgumentNotValidException e) {
        log.error("[参数校验异常]： ", e);
        return ShXResponseUtil.build(ErrorCode.ERROR400.getCode(), e.getBindingResult().getFieldError().getDefaultMessage(), null);
    }

    /**
     * 默认异常处理
     *
     * @param e 异常
     * @return 统一返回结果
     */
    @ExceptionHandler(IllegalStateException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public ShXResponse<String> exceptionHandler(IllegalStateException e) {
        return ShXResponseUtil.build(ErrorCode.ERROR400.getCode(), ErrorCode.ERROR400.getMsg(), null);
    }

    /**
     * 默认异常处理
     *
     * @param e 异常
     * @return 统一返回结果
     */
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public ShXResponse<String> exceptionHandler(HttpRequestMethodNotSupportedException e) {
        return ShXResponseUtil.build(ErrorCode.ERROR400.getCode(), ErrorCode.ERROR400.getMsg(), null);
    }


    /**
     * 自定义异常处理， 处理controller中返回的错误
     *
     * @param e 异常
     * @return 统一返回结果
     */
    /*@ExceptionHandler(ControllerException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public ResponseEntity<ShXResponse<String>> exceptionHandler(ControllerException e) {
        return new ResponseEntity<>(WVPResult.fail(e.getCode(), e.getMsg()), HttpStatus.OK);
    }*/

    /**
     * 登陆失败
     *
     * @param e 异常
     * @return 统一返回结果
     */
    /*@ExceptionHandler(BadCredentialsException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public ResponseEntity<WVPResult<String>> exceptionHandler(BadCredentialsException e) {
        return new ResponseEntity<>(WVPResult.fail(ErrorCode.ERROR100.getCode(), e.getMessage()), HttpStatus.OK);
    }*/
}